package com.degratnik.plitki.adapters;

import java.util.Date;
import java.util.List;

import com.degratnik.plitki.R;
import com.degratnik.plitki.fragments.Plitka;
import com.degratnik.plitki.sql.data.DataSource;
import com.degratnik.plitki.sql.data.Event;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PlitkiAdapter extends BaseAdapter {

	private DataSource source;
	private List<Event> events;

	public PlitkiAdapter(Context appContext) {
		if (appContext instanceof Activity) {
			Log.w(this.getClass().getCanonicalName(),
					"DO NOT USE ACTIVITY CONTEXT.");
		}
		source = new DataSource(appContext);
		// TODO make open/close management for source
		source.open();
		if (source.getAllEvents().size() == 0) {
			long now = (new Date()).getTime();
			source.createEvent("home", "go home", "1", now, now, 1);
			now = now + 1000000 * 60 * 60;
			source.createEvent("shop", "buy milk", "", now, now, 2);
			now = now + 1000000 * 60 * 60;
			source.createEvent("work", "make class", "1", now, now, 3);
			now = now + 1000000 * 60 * 60;
			source.createEvent("bd", "my birthday", "1", now, now, 4);
			now = now + 1000000 * 60 * 60;
			source.createEvent("shop", "buy sugar", "", now, now, 5);
			now = now + 1000000 * 60 * 60;
			source.createEvent("shop", "buy milk", "1", now, now, 6);
			now = now + 1000000 * 60 * 60;
			source.createEvent("shop", "buy milk", "", now, now, 7);
			now = now + 1000000 * 60 * 60;
			source.createEvent("shop", "buy milk", "1", now, now, 8);
			now = now + 1000000 * 60 * 60;
			source.createEvent("shop", "buy milk", "", now, now, 9);
			now = now + 1000000 * 60 * 60;
			source.createEvent("shop", "buy milk", "1", now, now, 10);
			now = now + 1000000 * 60 * 60;
			source.createEvent("shop", "buy milk", "", now, now, 11);
			now = now + 1000000 * 60 * 60;
			source.createEvent("shop", "buy milk", "1", now, now, 12);
			now = now + 1000000 * 60 * 60;
			for (int i = 0; i < 30; i++)
				source.createEvent("shop", "buy milk", "", now, now, 13);

		}
		events = source.getAllEvents();
	}

	@Override
	public int getCount() {
		return events.size() + 1;
	}

	@Override
	public Object getItem(int i) {
		return events.get(i - 1);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = View.inflate(parent.getContext(), R.layout.plitki_item, null);
		TextView title = (TextView) v.findViewById(R.id.title);
		ImageView image = (ImageView) v.findViewById(R.id.image);
		if (position == 0) {
			title.setText("");
			image.setImageResource(R.drawable.square_green);
		} else {
			Event e = (Event) getItem(position);
			title.setText(e.getTitle());
			
			image.setImageResource("1".compareTo(e.getImage()) == 0 ? R.drawable.square_blue
					: R.drawable.square_red);

		}
		image.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_DOWN
						|| event.getAction() == MotionEvent.ACTION_UP)
					Log.i(PlitkiAdapter.class.getName(),
							String.format("image touch %s", event.toString()));
				return false;
			}
		});
		return v;
	}

	public void swap(int swap_1, int swap_2) {
		if (swap_1 <= events.size() && swap_2 <= events.size()) {
			Event tmp = events.get(swap_1 - 1);
			events.set(swap_1 - 1, events.get(swap_2 - 1));
			events.set(swap_2 - 1, tmp);
		}

	}
	
	public void saveEvent(Event event) {
		source.updateEvent(event);
	}

}
